package lock;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 *  1000W数据性能测试
 *      单线程     5874/6155    6000
 *      悲观锁（synchronized）  5910/5655    5999/5561
 *      乐观锁（Lock，CAS算法,非阻塞）    5993/6124
 *      ConcurrentHashMap   5904/5608/5733
 *
 *      多线程不一定比单线程快，主要看数据量；
 * @author Caleb
 * @date 2021/3/22 10:07
 * @description
 */
public class MyBatisEnhance {



    private final static Integer size = 10000000;

    private static Map<Object, Object> mapperRegister = new ConcurrentHashMap<>(size);

    private static Lock lock = new ReentrantLock();

    private static CASTest casTest = new CASTest();


    public static List<Object> scannerMapper() {
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(new Object());
        }
        return list;
    }

    public static void put(Object obj){
        mapperRegister.put(obj,obj);
    }

    public static void main(String[] args) {
        List<Object> classes = scannerMapper();

        long start = System.currentTimeMillis();
        classes.parallelStream().forEach(MyBatisEnhance::put);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
